Day 7 - Sequences and counting

26

$ seq 4 -1 -5

This will give you the sequence of integers 4, 3, 2, ... -4, -5. As you probably guessed, you can

change the step arbitrarily, for example using 2 to skip every other number, or any other requirement

you might have, for example

$ seq 1 2 10

As you can read in the man page, the seq command has some command-line options. It is particularly

useful to learn the -w switch, that prepends enough 0s to keep all numbers at the same length. For

example

$ seq -w 1 10

prepends one 0 to the numbers between 1 and 9 to get the same length of the last number, 10. This

is called left zero-padding, in the elite circles of programmers, but I bet you can also call it zero left

padding and everybody would understand. Now shout “Launch a zero left-padded sequence!” and

tell me if you don’t feel like one the mecha pilots of some Japanese anime. I honestly thing it’s cool.

Another useful and simple thing we can learn today is counting elements. When it comes to lines,

words, or characters, often you need to know how many of them are in a file or in the output of a

command, and in those cases wc is your friend. Arguably, the name of the tool is not the best, but it

stands for “word count” and after a while you will get used to it, and it will no more conjure up any

idea of private spaces.

Despite of the name, wc can count several different things, lines and characters being the most useful

ones. Let’s test the line count with seq, using the -l options that makes wc output only the number

of lines

$ seq 1 10 | wc -l

10

The tool is very useful when we want to count the number of characters in a string

$ echo "This is a test" | wc -c

15

Wait a minute… 15? I count 14 characters there, including spaces. What’s going on? Well, you know

that echo adds a newline at the end of the string, right? That newline is a specific non-printable

character, which makes wc add one to the length. We discussed the -n option of echo that prevents

the newline, and using that you will get the correct length